﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GaiNo.Core.Extensions
{
    public static class DateTimeExtensions
    {
        public static string RemainTime(this DateTime startTime, DateTime endTime)
        {
            TimeSpan sp = endTime - startTime;
            if (endTime.CompareTo(startTime) < 0)
                sp = new TimeSpan(0, 0, 0, 0);
            return "{0}天{1}小时{2}分钟".FormatString(sp.Days, sp.Hours, sp.Minutes);
        }
        public static bool IsExpired(this DateTime expireTime)
        {
            return DateTime.Now > expireTime;
        }
        public static string ToRelativeDateTime(this DateTime dt)
        {
            TimeSpan ts = DateTime.Now - dt;
            if (ts.TotalSeconds < 1)
            {
                return "刚刚";
            }
            else if (ts.TotalMinutes < 1)
            {
                return ts.Seconds.ToString() + "秒前";
            }
            else if (ts.TotalHours < 1)
            {
                return ts.Minutes.ToString() + "分钟前";
            }
            else if (ts.TotalDays < 1)
            {
                return ts.Hours.ToString() + "小时前";
            }
            else if (ts.TotalDays < 7)
            {
                return ts.Days.ToString() + "天前";
            }
            else if (ts.TotalDays <= 28)
            {
                return (ts.Days / 7).ToString() + "星期前";
            }
            else
            {
                return dt.ToString("yyyy-MM-dd hh:mm");
            }
        }
    }
}
